/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.api.processor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONObject;

import edu.cmu.lti.sepia.api.json.JsonAction;
import edu.cmu.lti.sepia.data.db.PoolDAO;
import edu.cmu.lti.sepia.data.db.QAJudgmentResultDAO;
import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.data.db.TopicDAO;
import edu.cmu.lti.sepia.infra.eval.model.QAJudgmentResults;
import edu.cmu.lti.sepia.infra.topic.model.Topic;
import edu.cmu.lti.sepia.util.ExceptionUtil;
import edu.cmu.lti.sepia.util.ServletUtil;

public class QAEvalProcessor extends AbstractProcessor {
		
	@Override
	public JSONObject process( SQL sql, JsonAction action, HttpServletRequest request ) {
		ServletUtil servletUtil = new ServletUtil( request );
		
//    String sortingCriteria = servletUtil.readString("sortingCriteria", "score");
    String taskNameShort = servletUtil.readString( "taskNameShort", "" );
    String trgLang = servletUtil.readString( "trgLang", "" );
		int poolId = servletUtil.readInt("poolId",-1);
		int judgmentId = servletUtil.readInt("judgmentId",-1);
		String externalId = servletUtil.readString("externalId", "");
		int nuggetId = servletUtil.readInt("nuggetId", -1);
		int userId = servletUtil.readInt("userId", -1);
		int matched = servletUtil.readInt("matched",-1);
		
//		int done = servletUtil.readInt("done", -1);

    JSONObject json = new JSONObject();
    try {
      json.put("msg", "succeeded");

  		if ( action.equals( JsonAction.QAEvalPools ) ) {
        json = PoolDAO.getPoolJson( PoolDAO.getPools(sql, taskNameShort, false ) );
      } else if ( action.equals( JsonAction.QAEvalTopics ) ) {
        List<Topic> topics = TopicDAO.getTopics(sql, taskNameShort, null, -1);
        TopicDAO.generateExternalIds(topics,trgLang,4);
        TopicDAO.removeUnselected(topics);
        json = TopicDAO.convertTopics(topics);
      } else if ( action.equals( JsonAction.QAEvalSystemResponses ) ) {
  //      json = "{\"qa_judgments\": [" +
  //      "{ " +
  //      "\"systemResponse\": \"TEST SR1\", "+		
  //      "\"judgmentId\": \"1\", "+    
  //      "\"nuggets\": [" +
  //      "{\"nuggetId\":3261,\"matched\":0}, " +
  //      "{\"nuggetId\":3284,\"matched\":1}" +
  //      "], "+		
  //      " }, " +
  //      "{ " +
  //      "\"systemResponse\": \"TEST SR2\", "+		
  //      "\"judgmentId\": \"2\", "+    
  //      "\"nuggets\": [{\"nuggetId\":1,\"matched\":1}], "+		
  //      " }" +
  //      "]}";
        QAJudgmentResults results = QAJudgmentResultDAO.findResultsByTopicAndPool( sql, taskNameShort, externalId, poolId );
        json = QAJudgmentResultDAO.getQAJudgmentResults(results);
      } else if ( action.equals( JsonAction.QAEvalUpdate ) ) {
        QAJudgmentResultDAO.updateResult(sql, taskNameShort,
          judgmentId, nuggetId, userId, matched);
      } else if ( action.equals( JsonAction.QAEvalDone ) ) {
        
      }
  //		if ( action.equals( JsonAction.TopicBrief ) ) {
  //			dataSet = "testing"; // hack!
  //			json = TopicToJson.convertSingleTopic(QAEvalDAO.loadTopicBrief(dataSet, topicId));
  //		} else if ( action.equals( JsonAction.QAEvalPools ) ) {
  //			json = edu.cmu.lti.sepia.legacy.Json.getQAEvalPoolJson( lang, true );
  //		} else if ( action.equals( JsonAction.QAEvalAdditionalPools ) ) {
  //			json = edu.cmu.lti.sepia.legacy.Json.getQAEvalPoolJson( lang, false );
  //		} else if ( action.equals( JsonAction.QAEvalTopics ) ) {
  //			//request.getSession().setAttribute("poolId", poolId);
  //			json = edu.cmu.lti.sepia.legacy.Json.getQAEvalTopicJson(lang, poolId);
  //		} else if ( action.equals( JsonAction.QAEvalMain ) ) {
  //			json = edu.cmu.lti.sepia.legacy.Json.getQAEvalMainJson(poolId, topicId);
  //		} else if ( action.equals( JsonAction.QAInsertMatched ) ) {
  //			json = QAEvalDAO.submitMatch( request, systemResponseId, topicId, nuggetId , matched );
  //		} else if ( action.equals( JsonAction.QAEvalInsertDone ) ) {
  //			json = QAEvalDAO.submitDone( request, poolId, topicId, done ); 
  //		} else if ( action.equals( JsonAction.QAEvalDoneTopic ) ) {
  //			json = "{'done': "+QAEvalDAO.loadMatchingStatus(poolId, topicId)+" }"; 
  //		}
  			
	    } catch ( Exception e ) {
	      e.printStackTrace();
	      try {
	        json.put("exception", ExceptionUtil.exceptionToString(e));
	      } catch (Exception e2) {
	        e2.printStackTrace();
	      }
	    }
	    return json;
	  }
		
}
